#!/bin/bash


install_git(){
    if which opkg;then
        # opkg update
        # opkg install git
        opkg update
        # opkg remove git
        opkg install git-http
        opkg install ca-bundle
    elif which apt;then
        echo '[info] maybe need root'
        apt update
        apt install git
    fi
}


if ! which git;then
    install_git
    if ! which git;then
            echo "[E] install git failed"
    fi
fi



(
#  ###### settings
#     REPO_URL="https://github.com/Ftindy/IPTV-URL"
#     repo="iptv_url"
#  ############ end

    # init_file="${service_name}.inited.txt"

    # service_name="$(basename "$0")"


    dst="$(dirname "$0")/"
    # dst="/mnt/mmcblk2p4/"

    cd "$dst" # || echo "[E] $dst not exist" && exit 1


    # pre do
    bash  _download_live_txt.sh


    echo "[attention] this job exit with no git fell or pull"

    echo "if need force update  live txt, set ENV : FORCE=true "

    exit
################################################
################################################
################################################
################################################
################################################
################################################









        # if [ "$1" == "clone" ];then
        #         if [ ! -d "$repo" ];then
        #                 git clone "$REPO_URL" "$repo"
        #         else
        #                 if git clone "$REPO_URL" "tmp_repo";then
        #                         rm -rf "$repo"
        #                         mv "tmp_repo" "$repo"
        #                 elif [ -d "$repo/.git" ];then
        #                         (
        #                                 cd "$repo"
        #                                 git  reset --hard HEAD
        #                                 git  status
        #                         )
        #                 fi

        #                 rm "$init_file" 2>/dev/null
        #         fi
        # fi

        # if [ ! -d "$repo" ];then
        #         echo "[E] can't find repo : $repo"
        #         exit 1
        # fi

        # cd "$repo"

        # check(){
        #         # 获取远程仓库的信息
        #         git fetch --quiet

        #         # 比较本地当前分支与远程分支的差异
        #         upstream_commit=$(git rev-parse @{u})
        #         local_commit=$(git rev-parse HEAD)

        #         # 判断本地是否已经是最新的
        #         if [[ $upstream_commit == $local_commit ]]; then
        #                 echo "已是最新提交"
        #                 return 1
        #         else
        #                 echo "有新提交，执行git pull"
        #                 git reset --hard HEAD
        #                 git pull --quiet
        #                 return 0
        #         fi
        # }

        # mk_link(){
        #         local name
        #         name="$1"
        #         if [ -f "$name" ];then
        #                 ln -sf "$name" "____.txt"
        #                 return 0
        #         fi
        #         return 1
        # }

        # do_it(){
        #         local from_ to_
        #         echo "[I] do it"
        #         echo > bestv.txt
        #         {
        #                 local count
        #                 count=1
        #                 for i in `grep "^http.*" bestv.m3u`; do
        #                         echo "$i"  | sed -E "s/^h/CH_$count,h/g"
        #                         count=$((count+1))
        #                 done
        #         } >> bestv.txt
        #         if mk_link "bestv.txt";then
        #                 return
        #         fi


        # }


        # is_first(){
        #         if [ ! -e "../$init_file" ];then
        #                 return 0
        #         fi
        #         return 1
        # }

        # init_it(){
        #         date > "../$init_file"
        # }


        # if is_first;then
        #         echo "[info] 1st"
        #         do_it
        #         init_it
        # elif check;then
        #         echo "[info] update"
        #         do_it
        # elif [ "$1" == "force" ] ;then
        #         echo "[info] force update"
        #         do_it
        # fi

)
